<?php
defined('IN_TS') or die('Access Denied.');

class historyAdmin extends history{

    /*
     * 配置选项
     * */
    public function options(){
		$arrOptions = $this->findAll('history_options');
		foreach($arrOptions as $item){
			$strOption[$item['optionname']] = stripslashes($item['optionvalue']);
		}
        include template("admin/options");
    }
	
	/*
	 * 保存配置
	 */
    public function optionsdo(){
        //先清空数据
        $this->doSql("TRUNCATE TABLE `".dbprefix."history_options`");

        foreach($_POST['option'] as $key=>$item){

            $optionname = $key;
            $optionvalue = trim($item);

            $this->create('history_options',array(

                'optionname'=>$optionname,
                'optionvalue'=>$optionvalue,

            ));

        }

        $arrOptions = $this->findAll('history_options',null,null,'optionname,optionvalue');
        foreach($arrOptions as $item){
            $arrOption[$item['optionname']] = $item['optionvalue'];
        }

        fileWrite('history_options.php','data',$arrOption);
        $GLOBALS['tsMySqlCache']->set('history_options',$arrOption);

        qiMsg('修改成功！');
    }

    /*
     * 大事记列表
     * */
    public function historylist(){
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $url = SITE_URL.'index.php?app=history&ac=admin&mg=historylist&page=';
        $lstart = $page*20-20;
        $arrHistory = $this->findAll('history',null,'addtime desc',null,$lstart.',20');

        $historyNum = $this->findCount('history');
        $pageUrl = pagination($historyNum, 20, $page, $url);

        include template("admin/history_list");
    }

    /*
     * 审核
     * */
    public function isaudit(){
        $historyid = intval($_GET['historyid']);

        $strHistory = $this->find('history',array(
            'historyid'=>$historyid,
        ));

        if($strHistory['isaudit'] == 0){

            $this->update('history',array(
                'historyid'=>$historyid,
            ),array(
                'isaudit'=>1,
            ));

        }

        if($strHistory['isaudit'] == 1){

            $this->update('history',array(
                'historyid'=>$historyid,
            ),array(
                'isaudit'=>0,
            ));

        }

        qiMsg('操作成功！');
    }

    /*
     * 删除
     * */
    public function deletehistory(){
        $historyid=intval($_GET['historyid']);

        $strHistory = $this->find('history',array(
            'historyid'=>$historyid,
        ));

        unlink('uploadfile/history/'.$strHistory['photo']);

        $this->delete('history',array(
            'historyid'=>$historyid,
        ));

        $this->delete('history_comment',array(
            'historyid'=>$historyid,
        ));

        qiMsg('删除成功！');
    }

    public function cate(){
        $ts=$_GET['ts'];
        switch($ts){
            //分类列表
            case "list":

                $arrCate = $this->findAll('history_cate',null,'orderid desc');

                include template("admin/cate_list");

                break;

            //分类添加
            case "add":


                include template("admin/cate_add");

                break;

            case "add_do":

                $this->create('history_cate',array(

                    'catename'=>trim($_POST['catename']),
                    'orderid'=>intval($_POST['orderid']),

                ));


                header("Location: ".SITE_URL."index.php?app=history&ac=admin&mg=cate&ts=list");

                break;

            //分类删除
            case "del":

                $cateid = intval($_GET['cateid']);
                $historyNum = $this->findCount('history',array(
                    'cateid'=>$cateid,
                ));

                if($historyNum > 0){
                    qiMsg("此分类有记录存在，不允许删除！");
                }

                $this->delete('history_cate',array(
                    'cateid'=>$cateid,
                ));




                qiMsg("分类删除成功！");

                break;

            //分类修改
            case "edit":

                $cateid = intval($_GET['cateid']);

                $strCate = $this->find('history_cate',array(
                    'cateid'=>$cateid,
                ));


                include template("admin/cate_edit");

                break;

            //分类修改执行
            case "edit_do":
                $cateid = intval($_POST['cateid']);
                $catename = trim($_POST['catename']);

                $this->update('history_cate',array(
                    'cateid'=>$cateid,
                ),array(
                    'catename'=>$catename,
                    'orderid'=>intval($_POST['orderid']),
                ));

                header("Location: ".SITE_URL."index.php?app=history&ac=admin&mg=cate&ts=list");

                break;
        }
    }

}